/*
 * @Author: your name
 * @Date: 2021-06-26 09:52:06
 * @LastEditTime: 2021-06-26 11:05:58
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \6.26作业\yyds\conroller\list.js
 */
const query = require("../db/mysql")

const { success, error } = require("../model/model")

class Shop {
    getList = async (ctx) => {
        const { pageIndex, pageSize, keyword, sortBy, sortType } = ctx.query
        let sql = `select * from user where 1=1 `
        let sqlCount = `select count(id) as total from user where 1=1 `
        if (keyword) {
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%"`
        }
        if (sortBy && sortType) {
            sql += `order by ${sortBy} ${sortType} `
        }
        if (pageIndex && pageSize) {
            sql += `limit ${(pageIndex - 1) * pageSize},${pageSize} `
        }
        const data = await query(sql)
        const count = await query(sqlCount)
        ctx.body = success({
            data,
            total: count[0].total
        })
    }
    detailList = async (ctx) => {
        const { id } = ctx.params
        let sql=`select * from user where id="${id}"`
        const data=await query(sql)
        ctx.body=success(data)
    }
}

module.exports=new Shop()